package com.suket.jnn;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

public class LearningFunction {

  static Logger logger = Logger.getLogger(LearningFunction.class);

  public static boolean updateNewWeight( Weight weight , double learningRate ,
      double outputError , double outputLeftNode , double outputRightNode ) {
    boolean result = false;
    if ( weight == null ) {
      logger.log(Level.WARN, "Failed to update new weight "
          + ", found null weight object");
      return result;
    }
    weight.setW(calculateNewWeight(weight.getW(), learningRate, outputError,
        outputLeftNode, outputRightNode));
    result = true;
    return result;
  }

  public static double calculateNewWeight( double oldWeight ,
      double learningRate , double outputError , double outputLeftNode ,
      double outputRightNode ) {
    double w = oldWeight;
    w += learningRate * outputError * outputLeftNode * outputRightNode
        * ( 1 - outputRightNode );
    return w;
  }

}
